//
//  AppDelegate.m
//  speechManager
//
//  Created by dev on 15/10/29.
//  Copyright © 2015年 Chensh. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate () <NSTableViewDataSource, NSTableViewDelegate>

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

- (id)init {
    self = [super init];
    if (self) {
        _speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
        [_speechSynth setDelegate:self];
        
        _voices = [NSSpeechSynthesizer availableVoices];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        NSString *defaultVoice = [NSSpeechSynthesizer defaultVoice];
        NSInteger defaultRow = [_voices indexOfObject:defaultVoice];
        NSIndexSet *indices = [NSIndexSet indexSetWithIndex:defaultRow];
        [_tableView selectRowIndexes:indices byExtendingSelection:NO];
        [_tableView scrollRowToVisible:defaultRow];
        [_tableView reloadData];
    }
    return self;
}


- (IBAction)playButtonDidTouch:(id)sender {
    
    NSString *string = [_textField stringValue];
    
    if ([string length] == 0) {
        NSLog(@"string length zero length");
    }
    
    [_speechSynth startSpeakingString:string toURL:[[NSURL alloc] initFileURLWithPath:@"/Users/dev/Desktop/test.aiff"]];
    
    [_stopButton setEnabled:YES];
    [_playButton setEnabled:NO];
    [_tableView setEnabled:NO];
}

- (IBAction)stopButtonDidTouch:(id)sender {
    [_speechSynth stopSpeaking];
    [_stopButton setEnabled:NO];
    [_playButton setEnabled:YES];
    [_tableView setEnabled:YES];
}


- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking {
    NSLog(@"finished speaking = %d", finishedSpeaking);
    [_stopButton setEnabled:NO];
    [_playButton setEnabled:YES];
    [_tableView setEnabled:YES];
}





- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    NSLog(@"voice count: %ld", _voices.count);
    return _voices.count;
}

//- (id)tableView:(NSTableView *)aTableView
//objectValueForTableColumn:(NSTableColumn *)aTableColumn
//            row:(NSInteger)rowIndex {
//    
//    NSString *v = [_voices objectAtIndex:rowIndex];
//    return v;
//}

- (id)tableView:(NSTableView *)tv
objectValueForTableColumn:(NSTableColumn *)tableColumn
            row:(NSInteger)row
{
    NSString *v = [_voices objectAtIndex:row];
    NSDictionary *dict = [NSSpeechSynthesizer attributesForVoice:v];
    NSLog(@"v: %@", v);
    return [dict objectForKey:NSVoiceName];
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
    NSInteger row = [_tableView selectedRow];
    if (row == -1) {
        return;
    }
    
    NSString *selectedVoice = [_voices objectAtIndex:row];
    [_speechSynth setVoice:selectedVoice];
    NSLog(@"new voice = %@", selectedVoice);
}


@end
